package com.kamkor.interviewer.client.presenter;

import java.util.List;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.kamkor.interviewer.client.InterviewServiceAsync;
import com.kamkor.interviewer.client.Interviewer;
import com.kamkor.interviewer.client.LoadingIndicator;
import com.kamkor.interviewer.client.security.Authentication;
import com.kamkor.interviewer.client.view.TextChangedHandler;
import com.kamkor.interviewer.shared.QuestionAnswerDetails;

/**
 * 
 * @author kamkor
 *
 */
public class AnswerInterviewPresenter extends SecuredPresenter implements Saveable {	
	private final InterviewServiceAsync rpcService;
	private final Display display;
	private final HandlerManager eventBus;

	public interface Display extends DisplaysSaveInfo {
		FlexTable getQuestionAnswerTable();	
		HasClickHandlers getSaveAll();	
		void setAnswerTextChangeHandler(ChangeHandler handler);	
		void setData(List<QuestionAnswerDetails> list);
		List<QuestionAnswerDetails> getData(Long interviewId);
		void addToData(QuestionAnswerDetails questionAnswer);		
		Widget asWidget();	
	}
	
	public AnswerInterviewPresenter(InterviewServiceAsync rpcService, Display display, HandlerManager eventBus, 
			Long interviewId, Long key, Authentication authentication) {
		super(authentication, interviewId, key);
		this.rpcService = rpcService;
		this.display = display;
		this.eventBus = eventBus;
	}
	
	public void bind() {
		display.getSaveAll().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				doSaveAll();			
			}		
		});
		
		display.setAnswerTextChangeHandler(new TextChangedHandler(eventBus, display, Interviewer.constants.answerOrAnswerssAreNotSaved()));
		
		display.getIsSavedInfo().setText(Interviewer.constants.allSaved());
	}
	
	/**
	 * save all answers
	 */
	public void doSaveAll() {
		List<QuestionAnswerDetails> list = display.getData(getInterviewId());
		LoadingIndicator.loadingStarted(Interviewer.constants.saving());
		rpcService.updateQuestionAnswer(list, new AsyncCallback<Void>() {			
			public void onFailure(Throwable caught) {
				LoadingIndicator.loadingFinished();
			}			
			public void onSuccess(Void result) {				
				LoadingIndicator.loadingFinished();
				Label isSavedInfo = display.getIsSavedInfo();
				isSavedInfo.setText(Interviewer.constants.allSaved());
				isSavedInfo.setStyleName("textSaved");			
			}
		});
	}
	
	/** 
	 * Load QuestionAnswer from database and add them to flexTable 
	 */
	private void fetchQuestionAnswer(Long interviewId) {
		LoadingIndicator.loadingStarted("Trwa wczytywanie");
		rpcService.getQuestionAnswer(interviewId, new AsyncCallback<List<QuestionAnswerDetails>>() {			
			public void onFailure(Throwable caught) {
				LoadingIndicator.loadingFinished();
			}		
			public void onSuccess(List<QuestionAnswerDetails> result) {					
				display.setData(result);	
				LoadingIndicator.loadingFinished();
			}			
		});				
	}

	@Override
	public void authenticationFailed() {
			
	}

	@Override
	protected void goAuthenticated(HasWidgets container) {
		bind();
		container.clear();	
		container.add(display.asWidget());		
		fetchQuestionAnswer(getInterviewId());	
	}	
}
